package com.twq.oo.traits

/**
 * Created by tangweiqun on 2017/11/28.
 */
object TraitsDef {
  def main(args: Array[String]): Unit = {
    val frog = new Frog
    frog.philosophize()

    //定义一个trait也是定义一个类型
    val phil: Philosophical = frog
    phil.philosophize()
  }
}

//1、定义trait
//被其他的class进行mixed in
trait Philosophical {

  def method1(): Int

  //定义一个具体的方法
  def philosophize() = {
    println("I consume memory, therefore I am!")
  }
}

class Animal

trait HasLegs


//2、trait可以使用extends或者with来mixed in
//一个类可以mix in多个trait
class Frog extends Animal with Philosophical with HasLegs {

  override def method1(): Int = 2

  override def toString = "green"

  //3、可以重写掉trait中的方法
  override def philosophize() = {
    println("It ain't easy being " + toString + "!")
  }
}


//4、和class不一样的是，trait的定义不能含有参数
//trait NoPoint(x: Int, y: Int) // Does not compile
//class Point(x: Int)
